package GUI;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

@SuppressWarnings("serial")
/**
 * User's side bar containing all of its commands
 * @author Benny
 *
 */
public class UserSideBarGUI extends SideBar {

	/**
	 * Create the panel.
	 * @param M Instance of the main Frame of the program
	 */
	public UserSideBarGUI(MainFrame M) {
		super(M);
		
		
		JButton btnViewUserCard = new JButton("View User Card");
		btnViewUserCard.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//mf.userMain.viewUser.init();
				mf.userMain.viewUser.setVisible(true);
				mf.userMain.recentActivities.setVisible(false);
				mf.userMain.orderBook.setVisible(false);
				mf.userMain.extendBorrow.setVisible(false);
				mf.userMain.bookSearch.setVisible(false);
				mf.userMain.updateUser.setVisible(false);
				
				Integer id=mf.logged_in_user.id;
				mf.userMain.viewUser.textName.setText(mf.logged_in_user.username);
				mf.userMain.viewUser.textID.setText(id.toString());
				mf.userMain.viewUser.textEmail.setText(mf.logged_in_user.email);
				mf.userMain.viewUser.textPhone.setText(mf.logged_in_user.phone);
				mf.userMain.viewUser.textUserStatus.setText("Logged in");
				
				ArrayList<Object> send = new ArrayList<>();
				send.add("1");
				send.add("ViewBorrowsForUser");
				send.add(id);
				mf.client.accept(send);
				
                for(int i=0;i<mf.userMain.viewUser.searchTableModel1.getRowCount();i++)
                	 mf.userMain.viewUser.searchTableModel1.removeRow(i);
                if(mf.userMain.viewUser.searchTableModel1.getRowCount()==1)
                	mf.userMain.viewUser.searchTableModel1.removeRow(0);
                System.out.println("the size number is:");
                System.out.println(mf.userMain.viewUser.searchTableModel1.getRowCount());
			}
		});
		btnViewUserCard.setBounds(10, 24, 162, 40);
		add(btnViewUserCard);
		
		JButton btnUpdatUserCard = new JButton("Update User Card");
		btnUpdatUserCard.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mf.userMain.updateUser.init();
				mf.userMain.updateUser.setVisible(true);
				Integer id=mf.logged_in_user.id;
				mf.userMain.updateUser.textName.setText(mf.logged_in_user.username);
				mf.userMain.updateUser.textID.setText(id.toString());
				mf.userMain.updateUser.textEmail.setText(mf.logged_in_user.email);
				mf.userMain.updateUser.textPhone.setText(mf.logged_in_user.phone);
				
				mf.userMain.recentActivities.setVisible(false);
				mf.userMain.orderBook.setVisible(false);
				mf.userMain.extendBorrow.setVisible(false);
				mf.userMain.bookSearch.setVisible(false);
				mf.userMain.viewUser.setVisible(false);
			}
		});
		btnUpdatUserCard.setBounds(10, 75, 162, 40);
		add(btnUpdatUserCard);
		
		JButton btnExtendBorrow = new JButton("Extend Borrow");
		btnExtendBorrow.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mf.userMain.extendBorrow.init();
				mf.userMain.extendBorrow.setVisible(true);
				mf.userMain.recentActivities.setVisible(false);
				mf.userMain.orderBook.setVisible(false);
				mf.userMain.updateUser.setVisible(false);
				mf.userMain.bookSearch.setVisible(false);
				mf.userMain.viewUser.setVisible(false);
			}
		});
		btnExtendBorrow.setBounds(10, 126, 162, 40);
		add(btnExtendBorrow);
		
		JButton btnOrderBook = new JButton("Order Book");
		btnOrderBook.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mf.userMain.orderBook.init();
				mf.userMain.orderBook.setVisible(true);
				mf.userMain.recentActivities.setVisible(false);
				mf.userMain.extendBorrow.setVisible(false);
				mf.userMain.updateUser.setVisible(false);
				mf.userMain.bookSearch.setVisible(false);
				mf.userMain.viewUser.setVisible(false);
			}
		});
		btnOrderBook.setBounds(10, 177, 162, 40);
		add(btnOrderBook);
		
		JButton btnSearchBook = new JButton("Search Book");
		btnSearchBook.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mf.userMain.bookSearch.init();
				mf.userMain.bookSearch.setVisible(true);
				mf.userMain.recentActivities.setVisible(false);
				mf.userMain.extendBorrow.setVisible(false);
				mf.userMain.updateUser.setVisible(false);
				mf.userMain.orderBook.setVisible(false);
				mf.userMain.viewUser.setVisible(false);
				
				for (int i = mf.userMain.bookSearch.searchTableModel1.getRowCount() - 1; i >= 0; i--)
					 mf.userMain.bookSearch.searchTableModel1.removeRow(i);
				for (int i = mf.userMain.bookSearch.searchTableModel.getRowCount() - 1; i >= 0; i--)
					 mf.userMain.bookSearch.searchTableModel.removeRow(i);
			}
		});
		btnSearchBook.setBounds(10, 228, 162, 40);
		add(btnSearchBook);
		
		JButton btnRecentActivities = new JButton("Recent Activities");
		btnRecentActivities.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mf.userMain.recentActivities.setVisible(true);
				mf.userMain.bookSearch.setVisible(false);
				mf.userMain.extendBorrow.setVisible(false);
				mf.userMain.updateUser.setVisible(false);
				mf.userMain.orderBook.setVisible(false);
				mf.userMain.viewUser.setVisible(false);
				
				 for (int i = mf.userMain.recentActivities.searchTableModel.getRowCount() - 1; i >= 0; i--) 
					 mf.userMain.recentActivities.searchTableModel.removeRow(i);
                        
                 ArrayList<Object> send = new ArrayList<Object>();
                 send.add(mf.logged_in_user.role);
                 send.add("RecentActivity");
                 send.add(mf.logged_in_user.id);
                 mf.client.accept(send);
                 
                 for (int i = mf.userMain.recentActivities.searchTableModel1.getRowCount() - 1; i >= 0; i--) 
					 mf.userMain.recentActivities.searchTableModel1.removeRow(i);
                        
                 ArrayList<Object> send1 = new ArrayList<Object>();
                 send1.add(mf.logged_in_user.role);
                 send1.add("viewErrors");
                 send1.add(mf.logged_in_user.id);
                 mf.client.accept(send1);
                 
                 for (int i = mf.userMain.recentActivities.searchTableModel2.getRowCount() - 1; i >= 0; i--) 
					 mf.userMain.recentActivities.searchTableModel2.removeRow(i);
                        
                 ArrayList<Object> send2 = new ArrayList<Object>();
                 send2.add(mf.logged_in_user.role);
                 send2.add("viewOrders");
                 send2.add(mf.logged_in_user.id);
                 mf.client.accept(send2);
			}
		});
		btnRecentActivities.setBounds(10, 279, 162, 40);
		add(btnRecentActivities);
		
		ImageIcon img = new ImageIcon(getClass().getResource(
                "/util/grad.png"));
		JLabel picLabel = new JLabel(img);
		picLabel.setLocation(0, 0);
		picLabel.setSize(185, 580);
		add(picLabel);
	
	}
}
